package Utils;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

/**
 * 获取各种时间，时间间隔工作类
 * @author zgy
 *
 */
public class TimeHelper {
	/**
	 * 获取当前时间戳
	 * @return
	 */
	public static Timestamp getNowTimestamp() {
		Date date = new Date();
		Timestamp timestamp = new Timestamp(date.getTime());
		return timestamp;
	}
	/**
	 * 判断设备是否离线
	 * @return
	 */
	public static boolean isTerminalOff(Timestamp timestamp) {
		Calendar offTime = Calendar.getInstance();
		offTime.add(Calendar.MINUTE, -30);
		Calendar loginTime = Calendar.getInstance();
		loginTime.setTimeInMillis(timestamp.getTime());
		return offTime.after(loginTime);
	}
	
	/**
	 * 判断是否超过3秒反馈
	 * @param timestamp
	 * @return
	 */
	public static boolean isNowLate(Timestamp timestamp) {
		Calendar nowTime = Calendar.getInstance();
		Calendar lateTime = Calendar.getInstance();
		lateTime.setTimeInMillis(timestamp.getTime());
		lateTime.add(Calendar.SECOND, 3);
		return nowTime.after(lateTime);
	}
	/**
	 * 判断与当前时间是否超过秒数
	 * @param timestamp
	 * @param seconds
	 * @return
	 */
	public static boolean isNowLateSeconds(Timestamp timestamp,int seconds) {
		Calendar nowTime = Calendar.getInstance();
		Calendar lateTime = Calendar.getInstance();
		lateTime.setTimeInMillis(timestamp.getTime());
		lateTime.add(Calendar.SECOND, seconds);
		return nowTime.after(lateTime);
	}
	/**
	 * 判断是否开箱超时
	 * @param timestamp
	 * @return
	 */
	public static boolean isForceOpenLate(Timestamp timestamp) {
		if(timestamp==null){
			return true;
		}else{
			Calendar nowTime = Calendar.getInstance();
			Calendar lateTime = Calendar.getInstance();
			lateTime.setTimeInMillis(timestamp.getTime());
			return nowTime.after(lateTime);
		}
	}
	/**
	 * 当前是否超时 （分钟）
	 * @param timestamp
	 * @param minute
	 * @return
	 */
	public static boolean isNowLate(Timestamp timestamp,int minute) {
		Calendar nowTime = Calendar.getInstance();
		Calendar lateTime = Calendar.getInstance();
		lateTime.setTimeInMillis(timestamp.getTime());
		lateTime.add(Calendar.MINUTE, minute);
		return nowTime.after(lateTime);
	}
	/**
	 * 当前是否超时 （分钟）
	 * @param timestamp
	 * @param minute
	 * @return
	 */
	public static boolean isNowLate(long timestamp,int minute) {
		Calendar nowTime = Calendar.getInstance();
		Calendar lateTime = Calendar.getInstance();
		lateTime.setTimeInMillis(timestamp*1000);
		lateTime.add(Calendar.MINUTE, minute);
		return nowTime.after(lateTime);
	}
	
	/**
	 * 获取超时时间
	 * 
	 * @param now
	 * @param period
	 * @return
	 */
	public static long getDeadTime(Date now, int period) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(now);
		calendar.add(Calendar.MINUTE, period);
		return calendar.getTimeInMillis();
	}
	
	/**
	 * 格式化当前时间
	 * @return
	 * by zgy
	 */
	public static String formattingNow() {
		Calendar now = Calendar.getInstance();
		now.setTimeInMillis(new Date().getTime());
		StringBuffer buffer = new StringBuffer();
		buffer.append(now.get(Calendar.YEAR)+"年");
		buffer.append(now.get(Calendar.MONTH)+"月");
		buffer.append(now.get(Calendar.DAY_OF_MONTH)+"日");
		buffer.append(now.get(Calendar.HOUR_OF_DAY)+"时");
		buffer.append(now.get(Calendar.MINUTE)+"分");
		buffer.append(now.get(Calendar.SECOND)+"秒");
		return buffer.toString();
	}
	
	/**
	 * 发送短信格式化{date}参数
	 * @param timestamp
	 * @return
	 * by zgy
	 */
	public static String formattingTime(Timestamp timestamp) {
		Calendar now = Calendar.getInstance();
		now.setTimeInMillis(timestamp.getTime());
		StringBuffer buffer = new StringBuffer();
		buffer.append((now.get(Calendar.MONTH)+1)+"月");
		buffer.append(now.get(Calendar.DAY_OF_MONTH)+"日");
		return buffer.toString();
	}
}
